//引入组件库
var nf = sm("do_Notification");
var root = ui("$");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var communityUrl = require("url/community");
var qqHttp = require("helper/http");
var httpSilence = require("helper/httpSilence");
var helperOpen = require("helper/open");
var imgTool = require("helper/picture");
// 实例化一个BaiduLocation对象
var dingwei = sm("do_BaiduLocation");

// UI
var headerAout = ui("headerAout");
var aLayoutMap = ui("aLayoutMap");
var textFieldAddress = ui("textFieldAddress");

// 本页面-ajax业务请求url
var httpUrl = communityUrl.url.base.create;

var dingweiX, dingweiY;
var dingweiCity, dingweiAddress, dingweiDistrict;

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "创建社区",
})
header.refreshData();
var imgMax = 2;
/** **************处理-图片显示UI-end**************** */
var aLayoutImg = ui("aLayoutImg");
var aLayoutImgRoot = ui("aLayoutImgRoot");

var imgGridUI = aLayoutImgRoot.add("imgGridShower",
		"source://view/public/imgselect/imgGrid.ui", 20, 15);
var imgGridView = ui(imgGridUI + ".imgGrid");
var listdataImg = mm("do_ListData");
imgGridView.bindItems(listdataImg);
imgGridView.on("touch", function(data, e) {
	imgMax=imgMax+1;
	listdataImg.removeData([ data ]);
	imgGridView.refreshItems();
});
/** **************处理-图片显示UI-end**************** */

/** **************点击-添加图片-begin**************** */
aLayoutImg.on("touch", function(data, e) {
	if (imgMax==0) {
		return;
	}
	imgTool.imgMulti(imgMax, selectImgBack);// 选择多张图片
});

function selectImgBack(imgList) {
	imgMax=imgMax-imgList.length;
	listdataImg.addData(imgList.map(function(path) {
		return {
			"imgPath" : path
		}
	}));
	imgGridView.refreshItems();
}
/** **************点击-添加图片-end**************** */

aLayoutMap.on("touch", function() {// 打开展示地图
	helperOpen.open("source://view/area/map.ui");
});

var layout_main = ui("do_LinearLayout_main");
var layoutBtn = ui("layoutBtn");

var addcommunityName = layout_main.add("communityName",
		"source://view/reuse/textField.ui", 0, 0);
var communityName = ui("communityName");
communityName.bindData(hashdata);
hashdata.addData({
	"textFieldHint" : "社区名称"
});
communityName.refreshData();
var textFieldName = ui(addcommunityName + ".textField");

var addbtnCommunity = layoutBtn.add("btnCommunity",
		"source://view/reuse/button.ui", 0, 0);
var btnCommunity = ui("btnCommunity");
btnCommunity.bindData(hashdata);
hashdata.addData({
	"btnText" : "提交社区资料"
});
btnCommunity.refreshData();
btnCommunity.on("touch", "", 5000, function() {
	do_Page.hideKeyboard();
	if (dingweiAddress == '' || dingweiAddress == null) {
		nf.toast("请标注地图");
		return;
	}
	if (textFieldName.text.trim() == '' || textFieldName.text == null) {
		nf.toast("请填写社区名称");
		return;
	}
	if (listdataImg.getCount()<=0) {
		nf.toast("请上传社区2张图片");
		return;
	}
	var imgFiles = [];
	for (var i = 0; i < listdataImg.getCount(); i++) {
		imgFiles.push(listdataImg.getOne(i).imgPath);
	}
	nf.confirm("感谢您的支持，我们会审核资料真实性，现在您可以", "", "确定提交", "取消", function(data, e) {
		if (data == 1) {
			qqHttp.form(imgFiles, "community/base", function(imgDatas) {
				var string4imgs = imgDatas.path;// 返回图片云端路径（后部份）用分号隔开
				var params = {
						name : textFieldName.text,
						address : dingweiAddress,
						x : dingweiX,
						y : dingweiY,
						cityName : dingweiCity,
						districtName : dingweiDistrict,
						img:string4imgs,
						resume : ""
					}
				httpSilence.post(httpUrl, params, function(data) {
					nf.toast("申请成功");
					do_App.closePage({fromPage:"createCommunity"});
				})
			});
		}
	})
})

// 从新增页面返回,刷新数据
do_Page.on("result", function(data) {
	textFieldAddress.text="定位中，请稍等";
	if(null!=data.longitude){
		dingweiX = data.longitude;
		dingweiY = data.latitude;
		dingweiAddress = data.address;
		dingwei.reverseGeoCode(dingweiY, dingweiX, function(data1, e1) {
			dingweiCity = data1.city;
			textFieldAddress.text = dingweiAddress;
			dingweiDistrict = data1.district;
		});
	}
});
// 点击背景隐藏输入键盘
root.on("touch", function() {
	do_Page.hideKeyboard();
})